package com.hy.domain;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.validation.constraints.Size;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Tour {

    @Size(min = 2)
    private String name;

    @Size(min = 2)
    private String image;

    @Size(max = 40000)
    private String content;

    private Integer sort;
    
    private String keywords;
    
	private String description;

    public com.hy.domain.Tour getUp() {
        Tour tour = null;
        try {
            TypedQuery<Tour> query = entityManager.createQuery("from Tour t where t.id < :id order by t.id desc", Tour.class);
            query.setParameter("id", this.getId());
            query.setFirstResult(0).setMaxResults(1);
            tour = query.getSingleResult();
        } catch (Exception e) {
        }
        return tour;
    }

    public com.hy.domain.Tour getNext() {
        Tour tour = null;
        try {
            TypedQuery<Tour> query = entityManager.createQuery("from Tour t where t.id > :id", Tour.class);
            query.setParameter("id", this.getId());
            query.setFirstResult(0).setMaxResults(1);
            tour = query.getSingleResult();
        } catch (Exception e) {
        }
        return tour;
    }
    
    public static List<Tour> getTourOrderBySort(int first, int max) {
    	List<Tour> list = null;
    	try {
			TypedQuery<Tour> query = Hotel.entityManager().createQuery("from Tour order by sort asc", Tour.class);
			query.setFirstResult(first).setMaxResults(max);
			list = query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return list;
    }
}
